package de.smarthome.ColorManagement;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.larswerkman.colorpicker.ColorPicker;
import com.larswerkman.colorpicker.ColorPicker.OnColorChangedListener;
import com.larswerkman.colorpicker.OpacityBar;
import com.larswerkman.colorpicker.SVBar;

import de.smarthome.R;


public class ColorFragment extends DialogFragment implements OnColorChangedListener {

	private ColorPicker picker;
	private SVBar svBar;
	private OpacityBar opacityBar;
	private Button button;
	private Button noColorButton;
	private TextView text;

	public interface ColorSetter {
		void setColor(int value);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.colorfragment, container, false);

		picker = (ColorPicker) v.findViewById(R.id.picker);
		svBar = (SVBar) v.findViewById(R.id.svbar);
		opacityBar = (OpacityBar) v.findViewById(R.id.opacitybar);
		button = (Button) v.findViewById(R.id.applyColorButton);
		noColorButton = (Button) v.findViewById(R.id.noColorButton);

		text = (TextView) v.findViewById(R.id.choose);
		picker.addSVBar(svBar);
		picker.addOpacityBar(opacityBar);
		picker.setOnColorChangedListener(this);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				((ColorSetter) getTargetFragment()).setColor(picker.getColor());
				dismiss();
			}
		});
		noColorButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				((ColorSetter) getTargetFragment()).setColor(0);
				dismiss();
				
			}
		});

		return v;
	}

	@Override
	public void onColorChanged(int color) {
		text.setTextColor(picker.getColor());
		System.out.println(color);
	}


}
